package com.strelitzia.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strelitzia.entity.ChatEntity;
import com.strelitzia.service.ChatService;
import com.strelitzia.vo.ResultVo;
import com.strelitzia.vo.UploadFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @create 2023/12/20 10:17
 */
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatService chatService;

    @GetMapping("/list")
    public ResultVo list() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(chatService.getList());
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("获取聊天列表失败");
        }
        return resultVo;
    }

    @PostMapping("/add")
    public void add(Integer fid, String content) {
        chatService.addChat(fid, content);
    }

    @GetMapping("/read")
    public void read(Integer uid, Integer fid) {
        ChatEntity chatEntity = new ChatEntity();
        if (uid > fid) {
            chatEntity.setChatHistory(fid + "-" + uid);
        } else {
            chatEntity.setChatHistory(uid + "-" + fid);
        }
        chatEntity.setNoRead(0);
        chatService.update(chatEntity, new QueryWrapper<ChatEntity>()
                .eq("chat_history", chatEntity.getChatHistory()));
    }

    @GetMapping("/del/{fid}")
    public void del(@PathVariable Integer fid) {
        chatService.delChat(fid);
    }


}
